package com.sgb.kjwl.view.ui.fragment.self;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.databinding.DataBindingUtil;
import androidx.recyclerview.widget.LinearLayoutManager;

import android.os.Bundle;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.google.android.material.tabs.TabLayout;
import com.sgb.kjwl.AppFragment;
import com.sgb.kjwl.R;
import com.sgb.kjwl.databinding.FragmentMyInquiryLeaseBinding;
import com.sgb.kjwl.di.machine.DaggerMachineFragComponent;
import com.sgb.kjwl.di.machine.MachineFragModule;
import com.sgb.kjwl.model.entity.self.LeaseInquiryCountEntity;
import com.sgb.kjwl.view.ui.activity.manage.WebSiteInquiryActivity;
import com.swgk.core.util.PickSelectEntity;
import com.sgb.kjwl.model.entity.self.UserInquiryEntity;
import com.sgb.kjwl.view.ui.adapter.machine.MyInquiryLeaseAdapter;
import com.sgb.kjwl.view.widget.lease.LeaseAskMapPopWindow;
import com.sgb.kjwl.viewmodel.machine.MyInquiryLeaseFragModel;
import com.swgk.core.base.di.AppComponent;
import com.swgk.core.dialog.BasePopWindow;
import com.swgk.core.recyclerview.decoration.RecyclerCustomDecoration;
import com.swgk.core.xrecyclerview.XRecyclerView;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

/**
 * 租赁-我的函询
 */
public class MyInquiryLeaseFragment extends AppFragment {

    private FragmentMyInquiryLeaseBinding binding;

    private String classNo = "";
    private String statusNo = "";

    private LeaseAskMapPopWindow classPopWindow;
    private LeaseAskMapPopWindow typePopWindow;

    private int page = 1;
    private boolean isLoadMore = false;
    private MyInquiryLeaseAdapter adapter;
    private List<PickSelectEntity> classDatas;
    private List<PickSelectEntity> statusDatas;
    private int type = 0;//0为收到，1为发起
    private String modelType = "4";

    @Inject
    MyInquiryLeaseFragModel viewModel;

    public static MyInquiryLeaseFragment newInstance() {
        MyInquiryLeaseFragment fragment = new MyInquiryLeaseFragment();
        return fragment;
    }

    public void setTypeInquiry(int type) {
        this.type = type;
        adapter.setType(type);
        viewModel.getCount();
        isLoadMore = false;
        getData();
    }

    @Override
    public void onResume() {
        super.onResume();
        //解决从H5返回刷新数据
        if(type==0){
            isLoadMore = false;
            getData();
        }
    }

    @Override
    protected void setupFragmentComponent(AppComponent appComponent) {
        DaggerMachineFragComponent.builder()
                .appComponent(appComponent)
                .machineFragModule(new MachineFragModule(this))
                .build()
                .inject(this);
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        binding = DataBindingUtil.inflate(inflater, R.layout.fragment_my_inquiry_lease, container, false);
        return binding.getRoot();
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        setTabMode();
        initView();
        initData();
    }

    private void initView() {
        binding.classLayout.setOnClickListener(this);
        binding.typeLayout.setOnClickListener(this);
        if (adapter == null) {
            adapter = new MyInquiryLeaseAdapter(getActivity(), null);
        }
        adapter.setType(type);
        LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
        binding.recyclerView.setLayoutManager(layoutManager);
        binding.recyclerView.addItemDecoration(new RecyclerCustomDecoration(0, 0, 0,
                getResources().getDimension(R.dimen.dp1), getResources().getColor(R.color.color_F5F5F5)));
        binding.recyclerView.setAdapter(adapter);
        binding.recyclerView.setLoadingMoreEnabled(true);
        binding.recyclerView.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
                isLoadMore = false;
                getData();
                viewModel.getCount();
            }

            @Override
            public void onLoadMore() {
                isLoadMore = true;
                getData();
            }
        });

        adapter.setOnDetailListener(new MyInquiryLeaseAdapter.onDetailListener() {
            @Override
            public void detail(String id) {
                viewModel.getH5Url(id, type);
            }
        });

        classPopWindow = new LeaseAskMapPopWindow(getActivity());
        classPopWindow.setOnResultClick(new BasePopWindow.OnResultClick() {
            @Override
            public void result(Object type, Object key2, Object key3) {
                classNo = ((PickSelectEntity) type).getNo();
                binding.tvClass.setText(((PickSelectEntity) type).getProjectscale());
                isLoadMore = false;
                getData();
            }
        });

        typePopWindow = new LeaseAskMapPopWindow(getActivity());
        typePopWindow.setOnResultClick(new BasePopWindow.OnResultClick() {
            @Override
            public void result(Object type, Object key2, Object key3) {
                statusNo = ((PickSelectEntity) type).getNo();
                binding.tvType.setText(((PickSelectEntity) type).getProjectscale());
                isLoadMore = false;
                getData();
            }
        });

    }

    private void initData() {
        statusDatas = new ArrayList<>();

        PickSelectEntity entity0 = new PickSelectEntity();
        entity0.setNo("");
        entity0.setProjectscale("全部");
        PickSelectEntity entity1 = new PickSelectEntity();
        entity1.setNo("5");
        entity1.setProjectscale("已答复");
        PickSelectEntity entity2 = new PickSelectEntity();
        entity2.setNo("1");
        entity2.setProjectscale("未答复");

        statusDatas.add(entity0);
        statusDatas.add(entity1);
        statusDatas.add(entity2);

        typePopWindow.goodsTypeAdapter.setDefItem(0);

        viewModel.getCount();
        viewModel.getFilterTypeModel(modelType);
        getData();
    }

    public void setTabMode() {
        binding.leaseTab.removeAllTabs();
        binding.leaseTab.addTab(binding.leaseTab.newTab().setText("出租函询"));
        binding.leaseTab.addTab(binding.leaseTab.newTab().setText("求租函询"));
        binding.leaseTab.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                if (tab.getPosition() == 0) {
                    binding.tvClass.setText("出租类目");
                    modelType = "4";
                } else {
                    binding.tvClass.setText("求租类目");
                    modelType = "5";
                }
                adapter.setModelType(modelType);
                isLoadMore = false;
                viewModel.getFilterTypeModel(modelType);
                getData();
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });
    }

    //设置函询数量
    public void showCount(LeaseInquiryCountEntity entity) {
        try {
            if (type == 0) {
                binding.leaseTab.getTabAt(0).setText("出租函询(" + entity.getLeaseReply() + ")");
                binding.leaseTab.getTabAt(1).setText("求租函询(" + entity.getRenyReply() + ")");
                ((WebSiteInquiryActivity) getActivity()).setTextNum(entity.getLeaseReply() + entity.getRenyReply());
            } else {
                binding.leaseTab.getTabAt(0).setText("出租函询(" + entity.getLeaseAdvisory() + ")");
                binding.leaseTab.getTabAt(1).setText("求租函询(" + entity.getRentAdvisory() + ")");
                ((WebSiteInquiryActivity) getActivity()).setTextNum(entity.getLeaseAdvisory() + entity.getRentAdvisory());
            }
        } catch (Exception e) {
        }
    }

    private void getData() {
        page = isLoadMore ? page + 1 : 1;
        if (type == 0) {
            viewModel.getDataListMyReplay(classNo, statusNo, modelType, page);
        } else {
            viewModel.getDataListMyInquiry(classNo, statusNo, modelType, page);
        }

    }

    public void showClassTypeList(List<PickSelectEntity> list) {
        if (list != null) {
            list.add(0, new PickSelectEntity("", "全部"));
        }
        this.classDatas = list;
        classPopWindow.setData(classDatas);
        classPopWindow.goodsTypeAdapter.setDefItem(0);
    }

    public void showList(List<UserInquiryEntity> list) {
        binding.recyclerView.refreshComplete();
        if (isLoadMore) {
            binding.recyclerView.loadMoreComplete();
            if (list.size() > 0) {
                adapter.addDatas(list);
            } else {
                binding.recyclerView.setLoadingMoreEnabled(false);
            }
            binding.recyclerView.setVisibility(View.VISIBLE);
        } else {
            binding.recyclerView.refreshComplete();
            if (list.size() > 0) {
                binding.recyclerView.setVisibility(View.VISIBLE);
                binding.emptyView.setVisibility(View.GONE);
                adapter.setDatas(list);
                binding.recyclerView.setLoadingMoreEnabled(true);
            } else {
                adapter.setDatas(null);
                binding.recyclerView.setVisibility(View.GONE);
                binding.emptyView.setVisibility(View.VISIBLE);
            }
        }
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()) {
            case R.id.class_layout:
                if (null != classDatas && classDatas.size() > 0) {
                    showClassPop();
                } else {
                    viewModel.getFilterTypeModel(modelType);
                }
                break;
            case R.id.type_layout:
                if (null != statusDatas && statusDatas.size() > 0) {
                    showTypePop();
                }
                break;
        }
    }

    private void showClassPop() {
        classPopWindow.showPopwindow(binding.roomLayout, 0, 0);
    }

    private void showTypePop() {
        typePopWindow.showPopwindow(binding.roomLayout, 0, 0);
        typePopWindow.setData(statusDatas);
    }

}
